﻿-- =============================================
-- Author:		William Oellermann
-- Create date: 12/14/06
-- Description:	
-- =============================================
CREATE PROCEDURE [ServiceEngine].[CreateRuntimeServer]
	@Name nvarchar(400),	
	@Description ntext = null,
	@CatalogURI ntext = null,
	@CatalogBindingID uniqueidentifier = null,
	@NotificationURI ntext = null,
	@NotificationBindingID uniqueidentifier = null,
	@BrokerURI ntext = null,
	@BrokerBindingID uniqueidentifier = null,
	@ListenerURI ntext = null,
	@ListenerBindingID uniqueidentifier = null,
	@HostName ntext = null,
	@PolicyID uniqueidentifier = null,
	@ID uniqueidentifier OUTPUT,
	@ReceiveNotifications bit = 1
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	SET NOCOUNT ON;

	SET @ID = newid();

	BEGIN TRY
		BEGIN TRANSACTION

		INSERT INTO ServiceEngine.RuntimeServers (RuntimeServerID, CatalogURI,NotificationURI, BrokerURI, ListenerURI, CatalogBindingID, NotificationBindingID, BrokerBindingID, ListenerBindingID, HostName, ReceiveNotifications, PolicyID)
		VALUES (@ID, @CatalogURI, @NotificationURI, @BrokerURI, @ListenerURI, @CatalogBindingID,@NotificationBindingID, @BrokerBindingID, @ListenerBindingID, @HostName, @ReceiveNotifications, @PolicyID)

		BEGIN TRY
			INSERT INTO ServiceEngine.Names (EntityID, EntityTypeID, EntityName, EntityDescription)
			VALUES (@ID, 0, @Name, @Description)
		END TRY
		BEGIN CATCH

			SELECT 
				@ErrorMessage = 'Attempt to insert RuntimeServer with duplicate name failed',
				@ErrorSeverity = 11,
				@ErrorState = ERROR_STATE();

			RAISERROR (@ErrorMessage, 
					   @ErrorSeverity,
					   1);
		END CATCH

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH

END


